package gpinterpreterTests;

import java.io.BufferedReader;
import java.io.FileReader;
import gpinterpreter.vector.VecInterpreter;
import gpinterpreter.GPParser;
import gpinterpreter.vector.VecInstruction;
import static gpinterpreter.vector.VecSymbol.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import primitives.cluster.ClusterHead;
import primitives.cluster.ClusterUtils;
import primitives.graph.Graph;
import primitives.graph.Node;
import search.util.TreeLoader;

public class GPInterpreterTest {
	VecInterpreter interpreter;
	ClusterHead tree;
	Graph g;
	Node a,b,c,d,e,f;
	List<VecInstruction> program;
	@Before
	public void setUp() throws Exception {
		g = new Graph();
		a = new Node("a");
		b = new Node("b");
		c = new Node("c");
		d = new Node("d");
		e = new Node("e");
		f = new Node("f");
		
		g.addNode(a);
		g.addNode(b);
		g.addNode(c);
		g.addNode(d);
		g.addNode(e);
		g.addNode(f);
		
		tree = new ClusterHead(g);
		VecInstruction[] p = {
				new VecInstruction(MOVE,1,2),
				new VecInstruction(NOP),
				new VecInstruction(NOP)
		};
		program = Arrays.asList(p);
		interpreter = new VecInterpreter(program, tree);
	}



	@Test
	public void testIsHalted() {
		assertFalse(interpreter.isHalted());
		for(int i = 0; i<10000; i++)
			interpreter.step();
		assertTrue(interpreter.isHalted());
	}

	@Test
	public void testStep() {
		ClusterHead original = new ClusterHead(g);
		interpreter.step();
		assertTrue(original.getChildren().size() != tree.getChildren().size());
		//ClusterUtils.prettyPrintTree(tree);
		
		VecInstruction [] p = {
				new VecInstruction(MERGE,2,1),
				new VecInstruction(MERGE,2,3),
				new VecInstruction(MERGE,2,4),
				new VecInstruction(MERGE,2,5),
				new VecInstruction(MERGE,2,6),
				new VecInstruction(MERGE,2,1),
				new VecInstruction(MERGE,2,2)
		};
		tree = new ClusterHead(g);
		program = Arrays.asList(p);
		interpreter = new VecInterpreter(program,tree);
		while(!interpreter.isHalted())
			interpreter.step();
		ClusterUtils.prettyPrintTree(tree);
	}
        
        @Test
        public void testOnFig3() throws Exception{
            ClusterHead toCluster = null;
            try{
                toCluster = TreeLoader.loadTreeFromDot("/Users/mat/Dropbox/graphs/dot/fig3.dot");
            
            //ArrayList<Instruction> program = new ArrayList<Instruction>();
            List<VecInstruction> p = null;
            BufferedReader in = new BufferedReader(new FileReader("/Users/mat/Desktop/programs/_Users_mat_Dropbox_graphs_dot_fig3.dot.txt"));
            String program,line;
            program = "";
		while ((line = in.readLine()) != null) {
                    program += line + "\n";
                }
            p = GPParser.parse(program);
            }catch(Exception e){
                throw e;
            }
            
        }   
        

}
